<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>计算属性</title>
</head>
<body>
  <h1>计算属性：用来做计算的一个属性,特点：依赖缓存</h1>
  <div id="app">
    <p>总数量：{{totalNum()}}</p>
    <p>总数量：{{totalNum()}}</p>
    <p>总数量：{{totalNum()}}</p>
    <hr>
    <p>{{age}}</p>
    <p>总数量：{{totalNum1}}</p>
    <p>总数量：{{totalNum1}}</p>
    <p>总数量：{{totalNum1}}</p>
  </div>
  <script src="../vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        age: '18',
        cart: [
          {
            name: 'iphone 12',
            price: 4999,
            num: 1
          },
          {
            name: 'huawei p40 pro',
            price: 4699,
            num: 7
          },
          {
            name: 'mi 10',
            price: 4199,
            num: 6
          }
        ]
      },
      // 计算属性
      computed: {
        totalNum1() {
          console.log('computed');
          let num = 0;
          this.cart.forEach(item => {
            num += item.num
          })
          return num
        }
      },
      methods: {
        totalNum() {
          console.log('fn');
          let num = 0;
          this.cart.forEach(item => {
            num += item.num
          })
          return num
        }
      },
    })
  </script>
</body>
</html>